package org.yankun.exam4j.security.passport.service.impl;

import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.yankun.exam4j.common.daobase.DaoBase;
import org.yankun.exam4j.entity.Passport;
import org.yankun.exam4j.security.passport.service.PassportService;


@Service
public class PassportServiceImpl implements PassportService {
	
	@Resource
	private DaoBase<Passport> dao;
	
	@Override
	public Passport verifyUser(Passport passport) {
		
		Assert.notNull(passport, "Passport must not be null");
		
		String hql = "from Passport p where p.username=?";
		
		@SuppressWarnings("unchecked")
		List<Passport> passPortList = dao.getHibernateTemplate().find(hql, new Object[]{passport.getUsername()});
		
		if(null != passPortList && !passPortList.isEmpty()){
			Passport pass = passPortList.get(0);
			
			if(passport.getPassword()!=null&&passport.getPassword().equals(pass.getPassword())){
				return pass;
			}
		}
		return null;
	}

}
